
// weixinmao_house/pages/pub/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areamsg:true,
    decmsg:true,
    typemsg:true,
    roommsg:true,
    letwaymsg:true,
    housemap:true,
    ismaster: true,
    houseareaid:0,
    roomid:0,
    letway:0,
    housetype:0,
    decorate:'',
    title:'',
    saleprice:0,
    perprice:0,
    housestyle:'',
    area:'',
    floor:'',
    direction:'',
    year:'',
    address:'',
    housearea:'',
    special:'',
    houselabel:'',
    imagelist:[],
    lng: 0,
    lat: 0,
    uploadimagelist:['','','','','',''],
    true1: true,
    true2: true,
    true3: true,
    true4: true,
    true5: true,
    true6: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '中介发布出租房-' + wx.getStorageSync('companyinfo').name,
    })


    that.checkuser({
      doServices: function () {

        that.oldhouseinit();

      },
      doElseServices: function () {

        //初始化导航数据

        that.oldhouseinit();


      }


    });






  }, oldhouseinit: function (e) {
    var that = this;
    var userinfo = wx.getStorageSync('userInfo');

    app.util.request({
      'url': 'entry/wxapp/Getpubinit',
      data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid },
      success: function (res) {
        if (!res.data.message.errno) {
          if (res.data.data.ismaster == 0) {
            that.data.ismaster = false;
            that.setData({
              ismaster: that.data.ismaster
            })
          }
          that.setData({
            citylist: res.data.data.arealist,
          })
        }
      }
    });





    // 获取定位，并把位置标示出来
    app.getLocationInfo(function (locationInfo) {
      // console.log('map', locationInfo);
      that.setData({
        longitude: locationInfo.longitude
        , latitude: locationInfo.latitude
        , markers: [
          {
            id: 0
            , iconPath: "../../resource/images/marker_checked.png"
            , longitude: locationInfo.longitude
            , latitude: locationInfo.latitude
            , width: 30
            , height: 30
          }
        ]
      })
    })

    //set the width and height
    // 动态设置map的宽和高
    wx.getSystemInfo({
      success: function (res) {

        that.setData({
          map_width: res.windowWidth
          , map_height: res.windowWidth
          , controls: [{
            id: 1,
            iconPath: '../../resource/images/marker_checked.png',
            position: {
              left: res.windowWidth / 2 - 8,
              top: res.windowWidth / 2 - 16,
              width: 30,
              height: 30
            },
            clickable: true
          }]
        })
      }
    })



  },
  upload:function(e){
    var that = this;
    var e = e;
    that.checkuser({
      doServices: function () {

        that.doupload(e);

      },
      doElseServices: function () {

        //初始化导航数据

        that.doupload(e);


      }
    }
    )
  }
  ,
  
  toMessage: function (e) {
    wx.navigateTo({
      url: "/weixinmao_house/pages/message/index"
    })

  },
  doupload:function(e){


    var that = this;
    var id = parseInt(e.currentTarget.dataset.id);
  
    switch (id) {
      case 1:
        if (that.data.true1 == false)
          return;
        break;
      case 2:
        if (that.data.true2 == false)
          return;
        break;
      case 3:
        if (that.data.true3 == false)
          return;
        break;
      case 4:
        if (that.data.true4 == false)
          return;
        break;
      case 5:
        if (that.data.true5 == false)
          return;
        break;
      case 6:
        if (that.data.true6 == false)
          return;
        break;
     

      default:


    }

    var imgurl1, imgurl2, imgurl3, imgurl4, imgurl5, imgurl6
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
     
      
        switch (id) {
          case 1:
            imgurl1 = tempFilePaths;
            console.log(that.data.true1 );
            if (that.data.true1  == false)
              return;
            that.data.true1 = false;
            break;
          case 2:
            imgurl2 = tempFilePaths;
            that.data.true2 = false;
            break;
          case 3:
            imgurl3 = tempFilePaths;
            that.data.true3 = false;
            break;
          case 4:
            imgurl4 = tempFilePaths;
            that.data.true4 = false;
            break;
          case 5:
            imgurl5 = tempFilePaths;
            that.data.true5 = false;
            break;
          case 6:
            imgurl6 = tempFilePaths;
            that.data.true6 = false;
            break;

          default:


        }
 
        that.setData({
          imgurl1: imgurl1,
          imgurl2: imgurl2,
          imgurl3: imgurl3,
          imgurl4: imgurl4,
          imgurl5: imgurl5,
          imgurl6: imgurl6,
          true1:that.data.true1,
          true2: that.data.true2,
          true3: that.data.true3,
          true4: that.data.true4,
          true5: that.data.true5,
          true6: that.data.true6,

        })
   
        that.data.imagelist.push(tempFilePaths);
      
      //  upload(that, tempFilePaths);
        that.uploadimg(tempFilePaths , id);
      }
    })



 
  },

  delupload:function(e){

    var that = this;
    var id = parseInt(e.currentTarget.dataset.id);

    switch (id) {
      case 1:
        that.setData({ imgurl1: '', true1:true})
          
        break;
      case 2:
        that.setData({ imgurl2: '', true2: true })
       
        break;
      case 3:
        that.setData({ imgurl3: '', true3: true })
      
        break;
      case 4:
        that.setData({ imgurl4: '', true4: true })
        
        break;
      case 5:
        that.setData({ imgurl5: '', true5: true })
          
        break;
      case 6:
        that.setData({ imgurl6: '', true6: true })
         
        break;


      default:


    }

    for (var i = 0; i < this.data.uploadimagelist.length ; i++)
      {
        var j = i+1;
        if(j == id)
          {

          this.data.uploadimagelist[i]= '';
          }


      }
    console.log(this.data.uploadimagelist);


  },



  savepubinfo:function(e){

   // this.data.uploadimagelist = [];
    
    var userinfo = wx.getStorageSync('userInfo');

    var title = e.detail.value.title;
    var money = e.detail.value.money;
    var payway = e.detail.value.payway;
    var roomtype = e.detail.value.roomtype;
    var area = e.detail.value.area;
    var floor = e.detail.value.floor;
    var direction = e.detail.value.direction;
    var year = e.detail.value.year;
    var housearea = e.detail.value.housearea;
    var address = e.detail.value.address;
    var special = this.data.special;
    var houselabel = this.data.houselabel;
    
    var lng = this.data.lng;
    var lat = this.data.lat;

    if(this.data.houseareaid == 0)
      {
      wx.showModal({
        title: '提示',
        content: '请选择区域',
        showCancel: false
      })
      return

      }
    var houseareaid = this.data.houseareaid;
    if (this.data.decorate == '') {

      wx.showModal({
        title: '提示',
        content: '请选择装修',
        showCancel: false
      })
      return

    }
    var decorate = this.data.decorate;
    if (this.data.housetype == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择租房类型',
        showCancel: false
      })
      return

    }
    var housetype = this.data.housetype;

    if (this.data.roomid == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择房间数',
        showCancel: false
      })
      return

    }
    var roomid = this.data.roomid;

    if (this.data.letway == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择租房方式',
        showCancel: false
      })
      return

    }
    var letway = this.data.letway;
    

    

    
    if (lng == 0 || lat ==0 ) {
      wx.showModal({
        title: '提示',
        content: '请设置位置',
        showCancel: false
      })
      return

    }


 



    if (title == "") {
      wx.showModal({
        title: '提示',
        content: '请输入标题',
        showCancel: false
      })
      return
    }
    if (money == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租金',
        showCancel: false
      })
      return
    }
    if (payway == "") {
      wx.showModal({
        title: '提示',
        content: '请输入付款方式',
        showCancel: false
      })
      return
    }
    
    if (roomtype == "") {
      wx.showModal({
        title: '提示',
        content: '请输入房型',
        showCancel: false
      })
      return
    }
    if (area == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房面积',
        showCancel: false
      })
      return
    }
    if (floor == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房楼层',
        showCancel: false
      })
      return
    }
    if (direction == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房朝向',
        showCancel: false
      })
      return
    }
    if (year == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房年代',
        showCancel: false
      })
      return
    }
    if (housearea == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房小区',
        showCancel: false
      })
      return
    }
    if (address == "") {
      wx.showModal({
        title: '提示',
        content: '请输入租房地址',
        showCancel: false
      })
      return
    }

    

    if (!houselabel) {
      wx.showModal({
        title: '提示',
        content: '请选择租房特色',
        showCancel: false
      })
      return
    }
    if (!special) {
      wx.showModal({
        title: '提示',
        content: '请选择房源配套',
        showCancel: false
      })
      return
    }
  
    var uploadimagelist = this.data.uploadimagelist;
    if (uploadimagelist.length < 2) {
      wx.showModal({
        title: '提示',
        content: '上传图片不少于2张',
        showCancel: false
      })
      return

    }



    var uploadimagelist_str = uploadimagelist.join('@');
    var data = {
      sessionid: userinfo.sessionid,
       uid: userinfo.memberInfo.uid,
      uploadimagelist_str: uploadimagelist_str, 
                houseareaid: houseareaid, 
                decorate: decorate, 
                housetype: housetype, 
                roomid:roomid,
                letway:letway,
                longitude: lng,
                latitude: lat,
                title: title, 
                money: money, 
                payway: payway,
                roomtype: roomtype,
                area: area,
                floor: floor,
                direction: direction,
                year: year,
                housearea: housearea,
                address: address,
                special: special,
                houselabel: houselabel
                };
    app.util.request({
      'url': 'entry/wxapp/saveletpubinfo',
      data: data,
      success: function (res) {


        if (res.data.errno != 0) {
          // 登录错误 
          wx.hideLoading();
          wx.showModal({
            title: '失败',
            content: res.data.data.msg,
            showCancel: false
          })
          return;
        } else {

          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000,
            success: function (res) {
              console.log(res);
              wx.navigateTo({
                url: "/weixinmao_house/pages/myletpub/index"
              })
            }
          })
          /*
          wx.switchTab({
            url: '/weixinmao_house/pages/index/index',
          })
          */
        }



      }
    });





  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  showarea:function(e){

    this.data.areamsg = false;
    this.setData({
      areamsg: this.data.areamsg
    })


  },

  selectarea:function(e){
    this.data.houseareaid = e.currentTarget.dataset.id;
    var areaname = e.currentTarget.dataset.name;

    

    this.setData({
      areaname: areaname
    })
    this.closemsg();

  },
  selectdec: function (e) {
    this.data.decorate = e.currentTarget.dataset.id;
    var decorate = e.currentTarget.dataset.id;



    this.setData({
      decorate: decorate
    })
    this.closedec();

  },
  selecttype: function (e) {
    this.data.housetype = e.currentTarget.dataset.id;
    var housetype = e.currentTarget.dataset.id;
    var housetypename = e.currentTarget.dataset.name;



    this.setData({
      housetypename: housetypename
    })
    this.closetype();

  },

  selectroom: function (e) {
    this.data.roomid = e.currentTarget.dataset.id;
    var roomid = e.currentTarget.dataset.id;
    var roomname = e.currentTarget.dataset.name;



    this.setData({
      roomname: roomname
    })
    this.closeroom();

  },


  selectletway: function (e) {
    this.data.letway = e.currentTarget.dataset.id;
    var letway = e.currentTarget.dataset.id;
    var letwayname = e.currentTarget.dataset.name;



    this.setData({
      letwayname: letwayname
    })
    this.closeletway();

  },
  showdec:function () {
    this.data.decmsg = false;
    this.setData({
      decmsg: this.data.decmsg
    })


  },

  showtype: function () {
    this.data.typemsg = false;
    this.setData({
      typemsg: this.data.typemsg
    })


  },
  showroom: function () {
    this.data.roommsg = false;
    this.setData({
      roommsg: this.data.roommsg
    })


  },

  showletway: function () {
    this.data.letwaymsg = false;
    this.setData({
      letwaymsg: this.data.letwaymsg
    })


  },

  showmap: function () {
    this.data.housemap = false;
    this.setData({
      housemap: this.data.housemap
    })


  },
  getmap:function(){

      this.getLngLat();
  },
   getLngLat: function () {
    var that = this;
    this.mapCtx = wx.createMapContext("map4select");
    this.mapCtx.getCenterLocation({
      success: function (res) {
       // console.log(res);
        var map = res.longitude + ',' + res.latitude;
        that.data.lng = res.longitude ;
        that.data.lat = res.latitude;
        that.setData({
          longitude: res.longitude,
           latitude: res.latitude,
           housemap:true,
           map:map,
           markers: [
            {
              id: 0
              , iconPath: "../../resource/images/marker_checked.png"
              , longitude: res.longitude
              , latitude: res.latitude
              , width: 30
              , height: 30
            }
          ]
        })

      }
    })
  },


  closemsg: function (e) {

    this.data.areamsg = true;
    this.setData({
      areamsg: this.data.areamsg
    })

  },

  closetype: function (e) {

    this.data.typemsg = true;
    this.setData({
      typemsg: this.data.typemsg
    })

  },
  closeroom: function (e) {

    this.data.roommsg = true;
    this.setData({
      roommsg: this.data.roommsg
    })

  }, closeletway: function (e) {

    this.data.letwaymsg = true;
    this.setData({
      letwaymsg: this.data.letwaymsg
    })

  },

  
  closedec: function (e) {

    this.data.decmsg = true;
    this.setData({
      decmsg: this.data.decmsg
    })

  },
  closemap: function (e) {

    this.data.housemap = true;
    this.setData({
      housemap: this.data.housemap
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  uploadimg:function (path,id) {
    var uploadurl = app.util.geturl({ 'url': 'entry/wxapp/upload' });
    var id = id;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    });
    
    var that = this;
    wx.uploadFile({
      url: uploadurl,
      filePath: path[0],
      name: 'file',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'session_token': wx.getStorageSync('session_token')
      },
      success: function (res) {
        var getdata = JSON.parse(res.data);

        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }else{
        
        }
        var imgpath = getdata.data.path;


      //  var uploadimagelist = this.data.uploadimagelist;
       
        for (var i = 0; i < that.data.uploadimagelist.length; i++) {
          var j = i + 1;
          if (j == id) {

            that.data.uploadimagelist[i] = imgpath;
          }


        }




        //that.data.uploadimagelist.push(imgpath);
       // console.log(that.data.uploadimagelist);

        /*
        var data = res.data
        page.setData({  //上传成功修改显示头像
          src: path[0]
        })
        */
      },
      fail: function (e) {

        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast
      }
    })
  },
  checkboxChange: function (e) {
    var special = e.detail.value;
    this.data.special = special.join(',');
    //console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  checkboxChangehouse: function (e) {
    var houselabel = e.detail.value;
    this.data.houselabel = houselabel.join(',');
    //console.log('checkbox发生change事件，携带value值为：', e.detail.value)
  },
  checkuser: function (options) {


    var that = this;
    var options = options;
    var userinfo = wx.getStorageSync('userInfo');
    console.log(userinfo);
    if (!userinfo) {
      app.util.getUserInfo(
        function (userinfo) {
          that.getlethousedetail();
        }



      );
      return false;
    } else {
      if (!userinfo.memberInfo.uid) {
        app.util.getUserInfo();
        return false;
      } else {

        app.util.request({
          'url': 'entry/wxapp/checkuserinfo',
          data: { sessionid: userinfo.sessionid, uid: userinfo.memberInfo.uid },
          success: function (res) {
            console.log('payyyy');
            if (res.data.data.error == 0) {

              options.doServices();

            } else if (res.data.data.error == 2) {
              // console.log('payyyy');
              options.doElseServices();

            } else {




            }

          }
        });

      }
    }

  }


})


